﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SisLegal.Model;

namespace SisLegal.Conciliation
{
    public partial class ConciliationEditForm : System.Web.UI.Page
    {
        private int ID
        {
            get
            {
                if (Request.QueryString["id"] == null)
                    return 0;
                else
                    return int.Parse(Request.QueryString["id"]);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DDL_Participation_Seal.DataSource = Enumeration.GetAll<eParticipation_Seal_Fiscal_Folder>();
                DDL_Participation_Seal.DataTextField = "Value";
                DDL_Participation_Seal.DataValueField = "Key";
                DDL_Participation_Seal.DataBind();
                //---------------------------------------------------------
                Model.ConciliationManager manager = new Model.ConciliationManager();
                Model.Conciliation obj = manager.GetById(ID);

                this.txtNroConciliation.Text = obj.NroConciliation;
                this.txtCity.Text = obj.City;
                this.txtSolicitante.Text = obj.Solicitante;
                this.txtInvitado.Text = obj.Invitado;
                this.txtNroConciliation.Text = obj.NroConciliation;

                DDL_Participation_Seal.ClearSelection();
                DDL_Participation_Seal.Items.FindByValue(obj.ParticipationSeal.ToString()).Selected = true;

                this.txtDateInvitation.Text = String.Format("{0:dd/MM/yyyy}", obj.DateInvitation);
                this.txtFirstCitation.Text = String.Format("{0:dd/MM/yyyy}", obj.FirstCitation);
                this.txtSecondCitation.Text = String.Format("{0:dd/MM/yyyy}", obj.SecondCitation);

                this.txtSumilla.Text = obj.Sumilla;
                this.txtObservaciones.Text = obj.Observaciones;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            this.Error.Text = "Error : <br>";
            if (this.txtNroConciliation.Text == "")
            {
                this.Error.Text = " * Ingrese Numero de Conciliacion <br>";
                this.txtNroConciliation.Focus();
                return;
            }

            Model.ConciliationManager manager = new Model.ConciliationManager();
            Model.Conciliation obj = new Model.Conciliation();

            if (txtDateInvitation.Text == "") { txtDateInvitation.Text = "0001-01-01"; }
            if (txtFirstCitation.Text == "") { txtFirstCitation.Text = "0001-01-01"; }
            if (txtSecondCitation.Text == "") { txtSecondCitation.Text = "0001-01-01"; }

            obj.NroConciliation = this.txtNroConciliation.Text.Trim();
            obj.City = txtCity.Text.Trim();
            obj.Solicitante = txtSolicitante.Text.Trim();
            obj.Invitado = txtInvitado.Text.Trim();
            obj.ParticipationSeal = Convert.ToInt32(this.DDL_Participation_Seal.SelectedItem.Value);
            obj.DateInvitation = Convert.ToDateTime(this.txtDateInvitation.Text);
            obj.FirstCitation = Convert.ToDateTime(this.txtFirstCitation.Text);
            obj.SecondCitation = Convert.ToDateTime(this.txtSecondCitation.Text);
            obj.Sumilla = this.txtSumilla.Text.Trim();
            obj.Observaciones = this.txtObservaciones.Text.Trim();
            obj.ID = ID;
            manager.Update(obj);
            Response.Redirect("/Conciliation/ConciliationListView.aspx"); 
        }
    }
}